﻿using System;
using System.Collections.Generic;
using System.Text;
using GiftNet.Common;

namespace Carriage
{
    public class CarriageManager
    {
        public CarriageManager()
        { 
            
        }

        #region Propertys...
        private AppObject.TrafficType _trafficType = AppObject.TrafficType.EMS;
        /// <summary>
        /// 运输类型
        /// </summary>
        public AppObject.TrafficType trafficType
        {
            get
            {
                return _trafficType;
            }
            set
            {
                _trafficType = value;
            }
        }

        private double _discount = 1;
        /// <summary>
        /// 折扣
        /// </summary>
        public double discount
        {
            get
            {
                return _discount;
            }
            set
            {
                _discount = value;
            }
        }

        private EMSClass _EmsClass;
        /// <summary>
        /// EMS
        /// </summary>
        public EMSClass EmsClass
        {
            get
            {
                if (_EmsClass == null)
                {
                    _EmsClass = new EMSClass();
                }
                return _EmsClass;
            }
        }

        private DHLClass _dhlClass;
        /// <summary>
        /// EMS
        /// </summary>
        public DHLClass DHLClass
        {
            get
            {
                if (_dhlClass == null)
                {
                    _dhlClass = new DHLClass();
                }
                return _dhlClass;
            }
        }

        private AramexClass _aramexClass;
        /// <summary>
        /// EMS
        /// </summary>
        public AramexClass AramexClass
        {
            get
            {
                if (_aramexClass == null)
                {
                    _aramexClass = new AramexClass();
                }
                return _aramexClass;
            }
        }

        private UPSClass _upsClass;
        /// <summary>
        /// EMS
        /// </summary>
        public UPSClass UPSClass
        {
            get
            {
                if (_upsClass == null)
                {
                    _upsClass = new UPSClass();
                }
                return _upsClass;
            }
        }

        #endregion

        #region Methods...
        /// <summary>
        /// 计算运费
        /// </summary>
        /// <returns>返回单位为人民币</returns>
        public double GetCarriage(AppObject.TrafficType tt,double weight,double cubage,string country, int DhlAreaNumber, int UPSAreaNumber)
        {
            try
            {
                if (this.discount <= 0)
                {
                    throw new Exception("折扣不能小于等于零");
                }
                this.trafficType = tt;
                switch (this.trafficType)
                {
                    case AppObject.TrafficType.EMS:
                        return this.GetEMSCarriage(weight, cubage, country);
                    case AppObject.TrafficType.UPS:
                        if (country.ToLower().ToString() == "germany" || country.ToLower().ToString() == "france" || country.ToLower().ToString() == "poland" || country.ToLower().ToString() == "danmark" || country.ToLower().ToString() == "russia" || country.ToLower().ToString() == "Italy")
                        {
                            return GetUPSCarriage(weight, UPSAreaNumber) + 30;//如果属于上面的几个国家则每个国家运费加100
                        }
                        else
                            return GetUPSCarriage(weight, UPSAreaNumber);
                    //throw new Exception("UPS 方法未实现");
                    //break;
                    case AppObject.TrafficType.TNT:
                        return 0;

                    case AppObject.TrafficType.Aramex:
                        return this.GetAramexClassCarriage(weight, country);
                    //throw new Exception("TNT 方法未实现");
                    //break;
                    case AppObject.TrafficType.DHL:
                        //根据国家名获取国家DHL分布区号 
                        if (country.ToLower().ToString() == "germany" || country.ToLower().ToString() == "france" || country.ToLower().ToString() == "poland" || country.ToLower().ToString() == "danmark" || country.ToLower().ToString() == "russia" || country.ToLower().ToString() == "Italy")
                        {
                            return GetDHLCarriage(weight, DhlAreaNumber) + 30;//如果属于上面的几个国家则每个国家运费加100
                        }
                        else
                            return GetDHLCarriage(weight, DhlAreaNumber);
                    //throw new Exception("DHL 方法未实现");
                    //break;
                    case AppObject.TrafficType.Fedex:
                        return 0;
                    //throw new Exception("Fedex 方法未实现");
                    //break;
                    case AppObject.TrafficType.Sea_Freight:
                        return 0;
                    //throw new Exception("Sea_Freight 方法未实现");
                    //break;
                    case AppObject.TrafficType.Other:
                        return 0;
                    //throw new Exception("Other 方法未实现");
                    //break;
                    default:
                        break;
                }
                return 0;
            }
            catch
            {
                return 0;
            }
        }

        private double GetEMSCarriage(double weight, double cubage, string country)
        {
            this.EmsClass.weight = weight;
            this.EmsClass.CountryName = country;
            double rlt = this.EmsClass.GetCarriage() * this.discount;
            return rlt;
        }

        /// <summary>
        /// 返回ｄｈｌ运费
        /// </summary>
        /// <param name="weight">净重重量</param>　
        /// <param name="countryNumber">ｄｈｌ国家区域</param>
        /// <returns>返回ｄｈｌ人民币运费</returns>
        private double GetDHLCarriage(double weight, int countryNumber)
        {
            this.DHLClass.AreaNumber = countryNumber;
            this.DHLClass.weight = weight;
            double ret = DHLClass.GetCarriage();
            return ret;

        }
 
        private double GetAramexClassCarriage(double weight,string countrystring)
        {
            this.AramexClass.weight = weight;
            double ret = AramexClass.GetCarriage(countrystring);
            return ret;

        }

        /// <summary>
        /// 返回ups运费
        /// </summary>
        /// <param name="weight">净重重量</param>　
        /// <param name="countryNumber">ups国家区域</param>
        /// <returns>返回ups人民币运费</returns>
        private double GetUPSCarriage(double weight, int countryNumber)
        {
            this.UPSClass.AreaNumber = countryNumber;
            this.UPSClass.weight = weight;
            double ret = UPSClass.GetCarriage();
            return ret;

        }
 
        #endregion
    }
}
